package view;

import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

/**
 * @author polyphemus
 * This class contains a registry list of GameButtons for the GameWindow.
 */
public class GameButtonRegistry implements MouseListener {
	private List<GameButton> gameButtonList;
	private static final GameButtonRegistry instance = new GameButtonRegistry();
	/**Creates the singleton for this class with a GameButton List size of 10
	 * 
	 */
	private GameButtonRegistry() {	
		gameButtonList = new ArrayList<GameButton>(50);
	}
	
	/**This is a singleton class.  One could access the singleton with this getInstance() Method.
	 * @return
	 */
	public static GameButtonRegistry getInstance() {
		return instance;
	}
	/**Adds a GameButton to the internal GameButton List
	 * @param gameButton the GameButton to add to the registry
	 */
	public void addGameButton(GameButton gameButton) {
		gameButtonList.add(gameButton);
	}
	/**Removes a GameButton to the internal GameButton List
	 * @param gameButton the GameButton to remove from the registry
	 */
	public void removeGameButton(GameButton gameButton) {
		gameButtonList.remove(gameButton);
	}
	/**Checks if a give point is within the GameButton bounds
	 * This is only accessible by the Singleton
	 * @param gameButton the GameButton to remove from the registry
	 */
	private boolean isInButton(Point p, GameButton gameButton) {
		return gameButton.getRect().contains(p);
	}
	@Override
	public void mouseClicked(MouseEvent e) {
		for( int i = 0 ; i < gameButtonList.size() ; i++ ){
			if (isInButton(e.getPoint(), gameButtonList.get(i))) {
				gameButtonList.get(i).activateMouseClicked(e);
			}
		}
//		for(GameButton gb : gameButtonList) {
//			if (isInButton(e.getPoint(), gb)) {
//				gb.activateMouseClicked(e);
//			}
//		}
	}
	@Override
	public void mouseEntered(MouseEvent e) {
		for( int i = 0 ; i < gameButtonList.size() ; i++ ){
			if (isInButton(e.getPoint(), gameButtonList.get(i))) {
				gameButtonList.get(i).activateMouseEntered(e);
			}
		}
//		for(GameButton gb : gameButtonList) {
//			if (isInButton(e.getPoint(), gb)) {
//				gb.activateMouseEntered(e);
//			}
//		}
	}
	@Override
	public void mouseExited(MouseEvent e) {
		for( int i = 0 ; i < gameButtonList.size() ; i++ ){
			if (isInButton(e.getPoint(), gameButtonList.get(i))) {
				gameButtonList.get(i).activateMouseExited(e);
			}
		}
//		for(GameButton gb : gameButtonList) {
//			if (isInButton(e.getPoint(), gb)) {
//				gb.activateMouseExited(e);
//			}
//		}		
	}
	@Override
	public void mousePressed(MouseEvent e) {
		for( int i = 0 ; i < gameButtonList.size() ; i++ ){
			if (isInButton(e.getPoint(), gameButtonList.get(i))) {
				gameButtonList.get(i).activateMousePressed(e);
			}
		}
//		for(GameButton gb : gameButtonList) {
//			if (isInButton(e.getPoint(), gb)) {
//				gb.activateMousePressed(e);
//			}
//		}		
	}
	@Override
	public void mouseReleased(MouseEvent e) {
		for( int i = 0 ; i < gameButtonList.size() ; i++ ){
			if (isInButton(e.getPoint(), gameButtonList.get(i))) {
				gameButtonList.get(i).activateMouseReleased(e);
			}
		}
//		for(GameButton gb : gameButtonList) {
//			if (isInButton(e.getPoint(), gb)) {
//				gb.activateMouseReleased(e);
//			}
//		}		
	}
}
